<template>
    <div>
        <el-form :inline="true" :model="parameter" class="demo-form-inline">
            <el-form-item>
                <el-input v-model="parameter.name" placeholder="姓名" class="input"></el-input>
            </el-form-item>
            <el-form-item>
                <el-select v-model="parameter.state" placeholder="状态" class="input">
                    <el-option label="在职" :value="true"></el-option>
                    <el-option label="离职" :value="false"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-select v-model="parameter.department" placeholder="部门" class="input">
                    <el-option :label="item" :value="item" v-for="item in departmentList"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="info" @click="getStaffList">查询</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="oppenAddDialog">新增</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" border style="width: 100%" :header-cell-style="{'text-align':'center'}"
            :cell-style="{'text-align':'center'}">
            <el-table-column prop="id" label="ID" width="79">
            </el-table-column>
            <el-table-column prop="name" label="姓名">
            </el-table-column>
            <el-table-column prop="department" label="部门">
            </el-table-column>
            <el-table-column prop="entryTime" label="入职时间">
                <template slot-scope="scope">
                    {{ $moment(scope.row.entryTime).format('YYYY-MM-DD') }}
                </template>
            </el-table-column>
            <el-table-column prop="state" label="状态">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.state === true" type="success">在职</el-tag>
                    <el-tag v-else-if="scope.row.state === false" type="danger">离职</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="operation" label="操作">
                <template slot-scope="scope">
                    <el-button type="text" @click="getSatffInfo(scope.row.id)">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination @size-change="currentSize" @current-change="currPage" :pager-count="5"
            :page-size="parameter.pageSize" :page-sizes="[5, 10, 15]" :current-page="parameter.currentPage"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        <el-dialog title="编辑" :visible.sync="editDialogForm" :append-to-body="true" :close-on-click-modal="false"
            width="350px">
            <el-form :model="editForm" ref="edifForm">
                <el-form-item label="姓名" :label-width="formLabelWidth">
                    <el-input v-model="editForm.name" autocomplete="off" class="input"></el-input>
                </el-form-item>
                <el-form-item label="部门" :label-width="formLabelWidth">
                    <el-select v-model="editForm.department" class="input">
                        <el-option :label="item" :value="item" v-for="item in departmentList">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-radio-group v-model="editForm.state">
                        <el-radio :label="true">在职</el-radio>
                        <el-radio :label="false">离职</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editDialogForm = false">取 消</el-button>
                <el-button type="primary" @click="updateStaff()">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="新增" :visible.sync="addDialogForm" :append-to-body="true" :close-on-click-modal="false"
            width="350px">
            <el-form :model="addForm" ref="addForm" :rules="rules">
                <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
                    <el-input v-model="addForm.name" autocomplete="off" class="input"></el-input>
                </el-form-item>
                <el-form-item label="部门" :label-width="formLabelWidth" prop="department">
                    <el-select v-model="addForm.department" class="input">
                        <el-option :label="item" :value="item" v-for="item in departmentList">
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addDialogForm = false">取 消</el-button>
                <el-button type="primary" @click="addStaff()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            tableData: [],
            parameter: {
                name: '',
                state: '',
                department: '',
                currentPage: 1,
                pageSize: 5
            },
            formLabelWidth: '80px',
            total: 0,
            departmentList: [],
            editDialogForm: false,
            editForm: {
                name: '',
                department: '',
                state: ''
            },
            addDialogForm: false,
            addForm: {
                name: '',
                department: ''
            },
            rules: {
                name: [
                    { required: true, message: '姓名不能为空', trigger: 'blur' }
                ],
                department: [
                    { required: true, message: '部门不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        getStaffList() {
            this.$axios.post('staff/staff_list', this.parameter).then(res => {
                if (res.data.code === 200) {
                    this.tableData = res.data.data.staffList
                    this.total = res.data.data.total
                }
            })
        },
        getDepartmentList() {
            this.$axios.get('staff/department_list').then(res => {
                if (res.data.code === 200) {
                    this.departmentList = res.data.data
                    console.log(this.departmentList)
                }
            }) 
        },
        currentSize(newSize) {
            this.parameter.pageSize = newSize
            this.getStaffList()
        },
        currPage(newPage) {
            this.parameter.currentPage = newPage
            this.getStaffList()
        },
        getSatffInfo(id) {
            this.$axios.get('staff/SatffInfo/' + id).then(res => {
                this.editForm = res.data.data
                this.editDialogForm = true
            })          
        },
        updateStaff() {
            this.$axios.post('staff/updateStaff', this.editForm).then(res => {
                if (res.data.code === 200) {
                    this.$message.success(res.data.message)
                    this.editDialogForm = false
                    this.getStaffList()
                } else {
                    this.$meesage.error(res.data.message)
                }
            })
        },
        oppenAddDialog() {
            this.addDialogForm = true
        },
        addStaff() {
            this.$refs.addForm.validate(valid => {
                // 如果预验证通过就发起请求
                if (!valid) return
                this.$axios.post('staff/addStaff', this.addForm).then(res => {
                    if (res.data.code === 200) {
                        this.$message.success(res.data.message)
                        this.addDialogForm = false
                        this.getStaffList()
                    } else {
                        this.$message.error(res.data.message)
                    }
                })
            })
        }
    },
    created() {
        this.getStaffList()
        this.getDepartmentList()
    }
}
</script>

<style scoped>

.input {
    width: 200px;
}
</style>